<template>
<div class="vue-box">
  <div class="c-panel">
    <!-- 参数栏 -->
    <div class="c-title">检索参数</div>
    <el-form @submit.native.prevent>
      <sa-item v-model="p.search" type="text" name="模糊所属" />
      <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :def="{}" :clearable="true" />
      <div class="c-item">
        <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
      </div>
    </el-form>
    <!-- ------------- 快捷按钮 ------------- -->
    <sa-item type="fast-btn" show="add,get,export,reset" />
    <!-- 数据列表 -->
    <el-table ref="data-table" class="data-table" :data="dataList" border>
      <sa-td type="selection" width="45px" />
      <sa-td type="text" name="序号" prop="sort" width="80px" />
      <sa-td type="text" name="服务名" prop="name" />
      <sa-td type="text" name="识别码" prop="code" />
      <sa-td type="text" name="访问地址" prop="url" />
      <sa-td type="text" name="路由名" prop="viewName" />
      <sa-td type="text" name="域名" prop="domain" />
      <sa-td type="img" name="小图标" prop="icon" />
      <sa-td type="enum" name="是否授权" prop="isAuth" :jv="dims.YES_OR_NO" />
      <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
      <sa-td name="操作" fixed="right" width="300px">
        <template slot-scope="s">
          <el-button type="text" class="c-btn" @click="preview(s.row.url)">预览</el-button>
          <el-button type="text" class="c-btn" @click="update(s.row)">变更</el-button>
          <el-button type="text" class="c-btn" @click="setMenu(s.row)" :disabled="s.row.status === 1">服务权限
          </el-button>
          <el-button type="text" class="c-btn" @click="open(s.row)">
            <span>{{ s.row.status === 1 ? '运行中' : '已离线' }}</span>
          </el-button>
          <el-button type="text" class="c-btn" @click="del(s.row)" :disabled="s.row.status === 1">
            <span class="del-span">删除</span>
          </el-button>
        </template>
      </sa-td>
    </el-table>
    <!-- 分页 -->
    <div v-if="dataCount">
      <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: 'server-list',
  data() {
    return {
      p: { // 查询参数
        current: 1,
        size: 10,
        search: null,
        status: null,
        sortField: null
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 缓存字典
      dims: {},
    }
  },
  created() {
    this.dims = sa.$sys.getCurd()
    this.f5();
    // 监听表单回车执行查询
    sa.onInputEnter();
  },
  methods: {
    // 刷新
    f5: function () {
      sa.ajax('/server/getList', sa.removeNull(this.p), function (res) {
        // 数据
        this.dataList = res.data;
        // 数据总数
        this.dataCount = res.dataCount;
        // 刷新表格高度
        sa.f5TableHeight();
      }.bind(this), {
        type: "post"
      });
    },
    // 新增
    add() {
      sa.showModel('新增', () => import('./add'), {
        dims: this.dims
      });
    },
    // 变更
    update(data) {
      data.dims = this.dims
      sa.showModel('变更', () => import('./update'), data);
    },
    // 上线离线
    open(item) {
      sa.confirm('是否' + (item.status === 1 ? '下线' : '上线') + '服务，此操作需多次确认', function () {
        item.status === 1 ? item.status = 0 : item.status = 1
        sa.ajax('/server/update', sa.removeNull(item), function (res) {
          sa.ok(res.msg);
          // 刷新表格高度
          sa.f5TableHeight();
        }.bind(this), {
          type: "post"
        })
      }.bind(this));
    },
    // 预览
    preview(param) {
      param = this.formatUrl(param)
      window.open(param, '_blank');
    },
    // 地址格式化
    formatUrl(url) {
      if (url) {
        // 去除空格
        url = url.trim()
        // 移除首位符号
        const first = url.slice(0, 1);
        if (first !== '/') {
          url = '/' + url;
        }
        return url;
      }
    },
    // 删除
    del(item) {
      sa.confirm('是否删除【' + item.name + '】服务，此操作需多次确认', function () {
        sa.ajax('/server/delete', {
          id: item.id
        }, function (res) {
          sa.ok(res.msg);
          // 刷新表格高度
          sa.f5TableHeight();
        }.bind(this), {
          type: "get"
        })

      }.bind(this));
    },
    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), {
        id: data.id,
        dims: this.dims
      });
    },

    // 查看 - 根据选中的
    getBySelect: function (data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },
    // 配置当前服务菜单
    setMenu(data) {
      sa.showModel('配置服务菜单', () => import('./setting'), data);
    },
  },
}
</script>

<style scoped>
.el-tag {
  border-radius: 0px;
}

.del-span {
  color: rgb(221, 0, 0);
}
</style>
